#include"wireles.h"
////////////////////////////////////////////////////////////////////////////////
// VARIABLE DECLARATION
//
extern volatile char lastcommand[30];
unsigned int rx1_buffer[RXBUF_MAX];					//rx ontvangst buffer
volatile int rx1_buf_begin = 0;				//eerst ontvangen, (deze wordt altijd gelezen)
volatile int rx1_buf_end = 0;				//eerstvolgende lege locatie die gevuld gaat worden, (deze wordt altijd geschreven)
unsigned char state485=0x00;
unsigned char checksum485 = 0x00;
int lastFunction485[3];

/* This is UART1 receive ISR */

void __attribute__ ((interrupt, auto_psv))  _U1RXInterrupt(void)
{
	if (U1STAbits.PERR|U1STAbits.FERR|U1STAbits.OERR==1)
	{
		
	}
    //rx1_buf_end = U1RXREG;
  	rx1_buffer[ rx1_buf_end] = U1RXREG; 			// write in the buffer
  	if ( (rx1_buf_end+1)%RXBUF_MAX != rx1_buf_begin) 	// check if buffer full
  	rx1_buf_end++; 								// else increment buffer
    rx1_buf_end %= RXBUF_MAX; 						// wrap around
	IFS0bits.U1RXIF = 0;
}

void InitUART(void)
{
	
	    CloseUART1();
	    /* Configure uart1 receive and transmit interrupt */
	    /* Set Up UART */
		U1BRG=(Fcy/(16*57600))-1; /*31,5*/
		/* Change U1BRG to suit your clock frequency */
		IFS0bits.U1RXIF=1;			/*clear RX interrupt flag*/
		IPC2bits.U1RXIP=7;			/*set RX interrupt priority*/
		U1MODE=0b1000000000000000; 	/* Enable, 8data, no parity, 1 stop  */
		U1STA =0b1000010000000000; 	/* Enable TX */
		IEC0bits.U1RXIE=1;			/*enable RX interrupts*/
}

int charReceived1(void)
{
  if ( rx1_buf_begin == rx1_buf_end) 	// buffer empty
  return 0;
  else
  return 1;
}




int getChar1(void)
{

	if(charReceived1())
	{
		IEC0bits.U1RXIE 	= 0;		// set uart rx interupt 1 = aan 0 = uit
		// buffer must contain at least one key code
		int gotchar = rx1_buffer[ rx1_buf_begin++]; // extract the first key code
		rx1_buf_begin %= RXBUF_MAX; 	// wrap around the pointer
		IEC0bits.U1RXIE 	= 1;		// set uart rx interupt 1 = aan 0 = uit
		//INT2LCD(gotchar);				//alle ontvangen data naar display
		return gotchar;
	}
	else
	{
		// error buffer = empty
		return -1;
	}
}

void putU1(int c1)
{
	while ( U1STAbits.UTXBF); 			// wait while Tx buffer full
	U1TXREG = c1;	
	while (U1STAbits.TRMT==1);			//wacht tot u1txreg naar TSR gestuurd is
	while (U1STAbits.TRMT==0);			//wacht tot TSR leeg is
}

unsigned char receivedInTime1 (void)
	{
		int inttimeout=0;
		while((2000>inttimeout)|!charReceived1())
		{
			inttimeout++;
		}
		if (charReceived1()==1)
		{
			return 1;																	//als char ontvangen is 1
		}
		else return 0;																	//timeout, niks ontvangen
	}

unsigned int commandReceived1(void)
	{	//$0106100000*JJ
		unsigned int message_state;
		unsigned char NewByte,mlenght,mnumber,checksuma, checksumb,counter1;
		//Line1();
		//putsUART1("cR1");
		if (charReceived1())
		{
			//puts2LCD("a");
			NewByte = getChar1();
			if (NewByte!=-1)
			{
				/* Calculate checksum. */
				checksuma ^= NewByte;
				switch (state485)
				{
					case 0: /* Detect start of frame byte 1. */
						if (NewByte == '$')
						{
							//puts2LCD(",0");
							/* reset checksum. */
							checksuma = NewByte;
							/* Next state. */
							state485++;
							counter1++;
						}
						break;
					case 1: /* Address to. */
							//puts2LCD(",1");
						if (NewByte == '0')
						{
							/* Next state. */
							state485++;
							counter1++;
						}
						else
						{
							state485=0;
							counter1=0;
						}
						break;
					case 2: /* Address to. */
							//puts2LCD(",2");
						if (NewByte == '1')
						{
							/* Next state. */
							state485++;
							counter1++;
						}
						else
						{
							state485=0;
							counter1=0;
						}
						break;
					case 3: /* message lenght */
							//puts2LCD(",3");
						lastcommand[3]=NewByte-0x30;
						mlenght=lastcommand[3]*10;
						/* Next state. */
						state485++;
						counter1++;
						break;
					case 4: /* message lenght */
							//puts2LCD(",4");
						lastcommand[4]=NewByte-0x30;
						mlenght=mlenght+lastcommand[4];
						/* Next state. */
						state485++;
						counter1++;
						break;
					case 5: /* message number */
							//puts2LCD(",5");
						lastcommand[5]=NewByte-0x30;
						mnumber=lastcommand[5]*10;
						/* Next state. */
						state485++;
						counter1++;
						break;
					case 6: /* message number */
							//puts2LCD(",6");
						lastcommand[6]=NewByte-0x30;
						mnumber=mnumber+lastcommand[6];
						//counter1=6;						//counter1 gaat bijhouden hoeveel data hierna binnenkomt voor de boodschap eindigt
						/* Next state. */
						state485++;
						counter1++;
						break;
					case 7: /* receive message data */
						//puts2LCD(",7");
						if (NewByte != '*')
						{
							counter1++;					//er is data binnen dus ++
							lastcommand[counter1]=NewByte;
						}
						else
						{
							/* Next state. */
							state485++;
							counter1++;
						}
						break;
					case 8: /* checksum */
							//puts2LCD(",8");
						lastcommand[++counter1]=NewByte-0x30;
						checksumb=(NewByte-0x30)*10;
						/* Next state. */
						state485++;
						counter1++;
						break;
					case 9: /* checksum */
							//puts2LCD(",9");
						lastcommand[++counter1]=NewByte-0x30;
						checksumb=checksumb+(NewByte-0x30);
						if(1==1)			//if(checksuma==checksumb)
						{
							//puts2LCD("c");
							/* Next state. */
							state485++;
							counter1++;
						}
						else
						{
							counter1=0;
							state485=0;		//als de checksum niet klopt word de boodschap weggegooid
						}
						break;
					case 10: /* enter */
							//puts2LCD(",L");
						lastcommand[++counter1]=NewByte;
						/* Next state. */
						state485++;
						counter1++;
						break;
					case 11: /* enter */
							//puts2LCD(",l");
						lastcommand[++counter1]=NewByte;
						/* Next state. */
						state485++;
						counter1++;
						break;
					default:
						break;
				}
				if (state485 > 11)
				{
					//putU1('1'); //dit kan als crc feedback dienen
					//puts2LCD("e");
					state485 = 0;
					putU1('-');
					putU1(counter1);
					putU1('-');
					counter1 = 0;
					putU1(lastcommand[0]);
					putU1(lastcommand[1]);
					putU1(lastcommand[2]);
					putU1(lastcommand[3]);
					putU1(lastcommand[4]);
					putU1(lastcommand[5]);
					putU1(lastcommand[6]);
					putU1(lastcommand[7]);
					putU1(lastcommand[8]);
					putU1(lastcommand[9]);
					putU1(lastcommand[10]);
					putU1(lastcommand[11]);
					putU1(lastcommand[12]);
					putU1(lastcommand[13]);
					while(1)
					{
					}
					/* Yes. */
					return 1;
				}
				else
				{
					/* No. */
					
				}
			}
		}
		return 0;
	}

